package com.lizhe.springboot_jsp_shiro.config;

import com.lizhe.springboot_jsp_shiro.cache.RedisCacheManager;
import com.lizhe.springboot_jsp_shiro.realms.CustomerRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 类描述:
 *
 * @author Administrator
 * @create 2022-05-18 22:06
 */

@Configuration
public class ShiroConfig {

    //1.创建shiroFilter  //负责拦截所有请求
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager) {

        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //给filter设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        //配置系统受限资源
        //配置系统公共资源
        Map<String, String> map = new HashMap<String, String>();

        //map.put("/index.jsp","authc");//authc 请求这个资源需要认证和授权

        map.put("/user/login", "anon"); //anon 设置为公共资源 放行
        map.put("/user/register", "anon"); // 同上
        map.put("/register.jsp", "anon"); // 同上
        map.put("/user/getImage", "anon");

        map.put("/**", "authc"); // authc 其他的请求这个资源需要认证和授权!


        //默认认证界面路径 --- 当认证不通过时跳转的页面,不配置默认就是login
        shiroFilterFactoryBean.setLoginUrl("/login.jsp");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);

        return shiroFilterFactoryBean;
    }

    //2.创建安全管理器
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("getRealm") Realm realm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        //给安全管理器设置
        defaultWebSecurityManager.setRealm(realm);

        return defaultWebSecurityManager;
    }

    //3.创建自定义realm
    @Bean
    public Realm getRealm() {
        CustomerRealm customerRealm = new CustomerRealm();
        //设置hashed凭证匹配器
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        //设置md5加密
        credentialsMatcher.setHashAlgorithmName("md5");
        //设置散列次数
        credentialsMatcher.setHashIterations(1024);

        // 为realm设置匹配器
        customerRealm.setCredentialsMatcher(credentialsMatcher);


        // 开启缓存管理器 ehcache (认证一次存到库里，下次直接查缓存)
        // customerRealm.setCachingEnabled(true);
        // customerRealm.setAuthorizationCachingEnabled(true);
        // customerRealm.setAuthenticationCachingEnabled(true);
        // customerRealm.setCacheManager(new EhCacheManager());

        // 设置缓存管理器(redis)
        customerRealm.setCacheManager(new RedisCacheManager());
        // 开启全局缓存管理
        customerRealm.setCachingEnabled(true);
        // 开启认证的缓存管理
        customerRealm.setAuthenticationCachingEnabled(true);    // Authentication: 认证
        // 设置认证缓存的名字(redis里的key)
        customerRealm.setAuthenticationCacheName("authenticationCache");
        // 开启授权的缓存管理
        customerRealm.setAuthorizationCachingEnabled(true);     // Authorization: 授权
        // 设置授权缓存的名字
        customerRealm.setAuthorizationCacheName("authorizationCache");

        return customerRealm;
    }
}
